using System;
using System.IO;
using NUnit.Framework;
using MonsterNet;


namespace MonsterNetTests
{
	[TestFixture()]
	public class TestMNSynchronisedDirectory
	{
		private SetupTearDownDirectories std = new SetupTearDownDirectories();
		
		#region create/delete test file
		[TestFixtureSetUp]
		public void SetupDirectory() {
			std.SetupDirectory();
		}
		
		[TestFixtureTearDown]
		public void DeleteDirectory() {
			std.TearDownDirectory();			
		}
			
		#endregion
		
		private void ScanDir(DirectoryInfo dirInfo, MNSynchronisedDirectory syncDir, MNFSObjectLocation location, DateTime scanTime) {
			
            //scan for files
			bool newObject;
            foreach (FileInfo aFile in dirInfo.GetFiles()) {
                syncDir.SetChildFile(aFile, MNGlobal.DefaultDirectory(), location, scanTime, out newObject);
            }
			
            //scan for directories
            foreach (DirectoryInfo aDir in dirInfo.GetDirectories())
            {
				try
				{
                	MNSynchronisedDirectory newDir = syncDir.SetChildDirectory(aDir, MNGlobal.DefaultDirectory(), location, scanTime, out newObject);
					if(newDir != null) {
			            //scan for files in new dir
			            foreach (FileInfo aFile in aDir.GetFiles()) {
			                newDir.SetChildFile(aFile, MNGlobal.DefaultDirectory(), location, scanTime, out newObject);
			            }
					}
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.ToString());
				}
            }		
		}
		
		[Test()]
		public void TestCreate ()	{
			DirectoryInfo dirInfo = new DirectoryInfo(std.Pathname);

			DateTime scanTime = DateTime.Now;
			MNFSObjectLocation location = MNFSObjectLocation.Destination;

			MNSynchronisedDirectory syncDir = new MNSynchronisedDirectory(dirInfo, MNGlobal.DefaultDirectory(), location, scanTime);
			
			Assert.AreEqual(SetupTearDownDirectories.kTestRootDirName, syncDir.Name, @"syncDir Name");
			Assert.AreEqual(SetupTearDownDirectories.kTestRootDirName, syncDir.RootlessPath, @"syncDir rootless path");
			Assert.AreEqual(scanTime, syncDir.GetDateFoundForLocation(location));
			Assert.IsTrue(syncDir.GetExistsAtForLocation(location), @"exists at location destination");
			Assert.IsFalse(syncDir.GetExistsAtForLocation(MNFSObjectLocation.Portable), @"Exists at location portable");
			Assert.IsNotNull(syncDir.GetDateModifiedAsStringForLocation(location),@"date modified set");
			Assert.IsTrue(syncDir.Private, @"private is true");		
			
			syncDir.Ignore = true;
			
			Assert.IsTrue(syncDir.Ignore,@"Skip when scanning");
		}
		
		[Test()]
		public void TestSubDirsAndFiles() {
			DirectoryInfo dirInfo = new DirectoryInfo(std.Pathname);

			DateTime scanTime = DateTime.Now;
			MNFSObjectLocation location = MNFSObjectLocation.Destination;

			MNSynchronisedDirectory syncDir = new MNSynchronisedDirectory(dirInfo, MNGlobal.DefaultDirectory(), location, scanTime);
			
			ScanDir(dirInfo, syncDir, location, scanTime);
						
			Assert.AreEqual(2, syncDir.Files.Count, @"syncDir Files Count");
			Assert.AreEqual(2, syncDir.Directories.Count, @"syncDir Directories Count");
		
		}
		
		[Test()]
		public void TestSerialisation() {
			DirectoryInfo dirInfo = new DirectoryInfo(std.Pathname);
			DateTime scanTime = DateTime.Now;
			MNFSObjectLocation location = MNFSObjectLocation.Destination;

			MNSynchronisedDirectory syncDir = new MNSynchronisedDirectory(dirInfo, MNGlobal.DefaultDirectory(), location, scanTime);
			ScanDir(dirInfo, syncDir, location, scanTime);
			syncDir.Ignore = true;
			Assert.AreEqual(1, syncDir.Files.Count, @"syncDir Files Count");
			Assert.AreEqual(2, syncDir.Directories.Count, @"syncDir Directories Count");
			
			string _serialisedFilePathname = System.IO.Path.Combine(std.Pathname, @"TestMNSynchronisedDirectorySerialisation.MNSynchronisedDirectory");
			try {
				MNSerialise.SaveToFile<MNSynchronisedDirectory>(_serialisedFilePathname,syncDir);
				MNSynchronisedDirectory serialisedDir = MNSerialise.LoadFromFile<MNSynchronisedDirectory>(_serialisedFilePathname);
			
				Assert.AreEqual(syncDir.Name, serialisedDir.Name, @"serialisedDir Name");
				Assert.AreEqual(syncDir.Ignore, serialisedDir.Ignore, @"serialisedDir Skip when scanning");
				Assert.AreEqual(syncDir.Files.Count, serialisedDir.Files.Count, @"serialisedDir Files Count");
				Assert.AreEqual(syncDir.Directories.Count, serialisedDir.Directories.Count, @"serialisedDir Directories Count");				
				
			}
			finally {
				//File.Delete(_serialisedFilePathname);
			}
			
			
		}
		
		
		
	}
}

